package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * <电视台 - 南通电视台> 解析类
 *
 * @author KK
 *
 */
public class NtjoyAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {
		if(url.indexOf("/news/vod/zhpd/nttv/")!=-1||
				url.indexOf("/news/vod/sjpd/zeyz/")!=-1
				
		){
			return false;
		}

		String[] regex = {
				"http://www.ntjoy.com/.*vod/xwsph/.*/[0-9]{4}/[0-9]{2}/[0-9]{4}-[0-9]{2}-[0-9]*.html",
				};

		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		Long date = null;
		String dateStr = null;
		String commnetNum = null;
		String clickNum = null;
		String updateUrl = null;
		Document doc = Jsoup.parse(htmltxt);

		title =   doc.select("div.container > div.newsleft690 > div.miantopword > ul > li > span").text();
		dateStr = doc.select("div.container > div.newsleft690 > div.miantopword > ul > li > p").text();
//		clickNum = doc.select("span#hitsHTML").text();
//		commnetNum = doc.select("span#commentHTML").text();
		if(StringUtils.isNullOrEmpty(title)){
//			title =   	 doc.select("div.container_body div.hy_div_left4 div.disp_title").text();
//			dateStr = 	 doc.select("div.container_body div.hy_div_left4 div.sj_font").text();
			commnetNum = doc.select("div.container_body div.hy_div_left4 div a span").text();
		}
		date = DateUtils.matchDate(dateStr);
		if(StringUtils.isBlank(title)){
			title=doc.select("html body div.container div.newsleft690 div.miantopword h1").text();
		}
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.miantopword p span").html());
		}
		if(date==null){
			date=DateUtils.matchDate(url);
		}
		Elements remove = doc.select("div.lbyones> p:contains(版权声明：本网所有内容)").remove();
		String desc = HtmlCleaner.getContentHtml(url,doc.select("div.lbyones>p"));

			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(2);
			news.setContent(desc);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setDate(date);

			String author=null;
			author=doc.select("div.miantopword p span").text();
			if(author!=null && author.indexOf("来源：")!=-1){
				author=StringUtils.substringAfter(author, "来源：");
			}
			news.setAuthor(author);
			return news;

	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	

	
}
